/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package http.file.rbt;
import http.file.index.HttpFileIndexServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @description 基于http1.0支持断点续传下载的功能,实现客户端下载部分文件,无需下载文件的全部
 * 客户端发送请求返回返回 Accept-Ranges: bytes Content-Length: 100 表示支持Range,单位是字节,文件长度100
 * 客户端可以发送Range,请求下载资源
 * HTTP Range 属性
 * bytes = xxx-xxx 从xxx到xxx
 * bytes = xxx- 从xxx开始到以后的所有字节
 * bytes = -xxx 最后xxx个字节
 * bytes = xxx-xxx,xxx-xxx 同时指定多个范围
 * 例如：Range: bytes=0-1023
 * 服务端返回
     * HTTP/1.1
     * 206 Partial Content
     * Content-Range: bytes 0-1023/146515
     * Content-Length: 1024
     * (binary content)
 * **/
public final class HttpFileDownServer {

    static final int PORT = 8080;//端口号

    public static void main(String[] args) throws Exception {
        /**
         *netty一般写法开启一个服务
         * **/
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new HttpFileDownServerInitializer());
            Channel ch = b.bind(PORT).sync().channel();
            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
